home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Code Resources / cicnButton CDEF 1.3.2 / cicnButton.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  4.9 KB  |  202 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     cicnButton CDEF
  4.     version 1.3.2
  5.     
  6.     Written by: Paul Celestin
  7.     
  8.     Copyright © 1993-1997 Celestin Company, Inc.
  9.     
  10.     This CDEF displays a cicn whose resource ID is derived from the
  11.     value field of the CNTL. The min, max, and refcon fields are not
  12.     used and should therefore not be set.
  13.     
  14.     No parameters required!
  15.     
  16.     940320 - 1.0.0 - initial release
  17.     940531 - 1.0.1 - Changed to work on machines without color Quickdraw
  18.              The icon family needs two B&W additions which are ICON resources
  19.              rather than cicns.
  20.     951215 - 1.3.0 - updated for CW7
  21.     960704 - 1.3.1 - updated for CW9
  22.     970815 - 1.3.2 - updated for CW Pro 1
  23.  
  24. ---------------------------------------------------------------------- */
  25.  
  26. #include <Types.h>
  27. #include <Memory.h>
  28. #include <Quickdraw.h>
  29. #include <Fonts.h>
  30. #include <ToolUtils.h>
  31. #include <Icons.h>
  32. #include <Controls.h>
  33. #include <Gestalt.h>
  34.  
  35. #define INVERTED_ICON    1
  36.  
  37.  
  38. /* ----------------------------------------------------------------------
  39. prototypes
  40. ---------------------------------------------------------------------- */
  41. pascal long main(short, ControlHandle, short, long);
  42. void drawIt(ControlHandle, short);
  43. long testIt(ControlHandle, Point);
  44.  
  45.  
  46. /* ----------------------------------------------------------------------
  47. main
  48. ---------------------------------------------------------------------- */
  49. pascal long main (short variation, ControlHandle theControl, short message, long param)
  50.  
  51. {
  52.     long    returnValue = 0L;
  53.     char    state = HGetState((Handle)theControl);
  54.     Str255    copyright = "\pCopyright © 1993-1996 Celestin Company, Inc.";
  55.  
  56.     switch(message)
  57.     {
  58.         case drawCntl:
  59.             drawIt(theControl,variation);
  60.         case testCntl:
  61.             returnValue = testIt(theControl, *(Point *) ¶m);
  62.         case calcCRgns:
  63.             break;
  64.           case initCntl:
  65.               break;
  66.         case dispCntl:
  67.             break;
  68.         case posCntl:
  69.             break;
  70.         case thumbCntl:
  71.             break;
  72.         case dragCntl:
  73.             break;
  74.         case autoTrack:
  75.             break;
  76.         case calcCntlRgn:
  77.             break;
  78.         case calcThumbRgn:
  79.             break;
  80.         default:
  81.             break;
  82.     }
  83.  
  84.     HSetState((Handle)theControl,state);
  85.  
  86.     return(returnValue);                /* tell them what happened */
  87. }
  88.  
  89.  
  90. /* ----------------------------------------------------------------------
  91. drawIt - here is where we actually draw the control
  92. ---------------------------------------------------------------------- */
  93. static void drawIt (ControlHandle control, short variation)
  94. {
  95.     Rect             myRect;
  96.     short             myICONID;
  97.     CIconHandle        myICON;
  98.     Handle            myBWICON;
  99.     GrafPtr            myPort;
  100.     Pattern            myGray;
  101.     PenState        oldPenState;
  102.     Str255            myTitle;
  103.     int                savedFont,
  104.                     savedSize,
  105.                     savedMode;
  106.     OSErr            myErr;
  107.     Boolean            hasColorQD;
  108.     long            feature;
  109.  
  110.     GetPort(&myPort);                            /* save off the current port */
  111.  
  112.     if (!(*control)->contrlVis)                    /* if not visible, do nothing */
  113.         return;
  114.         
  115.                                                 /* find out if Color QD is present */
  116.     myErr = Gestalt(gestaltQuickdrawFeatures, &feature);
  117.     hasColorQD = ((myErr == noErr) && BitTst(&feature, 31 - gestaltHasColor));
  118.  
  119.     myICONID = GetCtlValue(control);            /* base ID is stored in the value field */
  120.     
  121.     if ((*control)->contrlHilite == inButton)
  122.         myICONID = myICONID + INVERTED_ICON;    /* invert while tracking */
  123.     
  124.     myRect = (*control)->contrlRect;            /* get the rectangle of the control */
  125.         
  126.     if ( hasColorQD )
  127.     {
  128.         myICON = GetCIcon(myICONID);            /* get the appropriate cicn resource */
  129.     
  130.         if ( myICON == 0L )                        /* make sure the cicn exists */
  131.             return;
  132.     
  133.         EraseRect(&myRect);                        /* erase before drawing */
  134.         
  135.         PlotCIcon(&myRect,myICON);                /* draw the cicn */        
  136.     }
  137.     else                                        /* no color QD case */
  138.     {
  139.         myBWICON = GetIcon(myICONID);
  140.  
  141.         if ( myBWICON == 0L )
  142.             return;
  143.  
  144.         EraseRect(&myRect);
  145.  
  146.         PlotIcon(&myRect, myBWICON);
  147.     }
  148.     
  149.     savedFont = myPort->txFont;                    /* save of values before drawing title */
  150.     savedSize = myPort->txSize;
  151.     savedMode = myPort->txMode;
  152.  
  153.     TextFont(geneva);                            /* change font to Geneva 9 point */
  154.     TextSize(9);
  155.     TextMode(srcOr);
  156.     BlockMove(((*control)->contrlTitle),myTitle,((*control)->contrlTitle)[0] + 1);            
  157.     MoveTo((myRect.right+myRect.left) / 2 - StringWidth(myTitle) / 2,myRect.bottom + 12);
  158.     DrawString(myTitle);                        /* draw the title */
  159.  
  160.     TextFont(savedFont);                        /* restore the values that were saved */
  161.     TextSize(savedSize);
  162.     TextMode(savedMode);
  163.  
  164.     switch (variation)
  165.     {
  166.     default:
  167.         break;
  168.     }
  169.  
  170.     if ((*control)->contrlHilite == 255)        /* gray out the cicn */
  171.     {
  172.         GetPenState(&oldPenState);
  173.         PenNormal();
  174.         GetIndPattern(&myGray,0,4);
  175.         PenPat(&myGray);
  176.         PenMode(patBic);
  177.         PaintRect(&myRect);
  178.         SetPenState(&oldPenState);
  179.     }
  180. }
  181.  
  182.  
  183. /* ----------------------------------------------------------------------
  184. testIt - test for mouse hits within control
  185. ---------------------------------------------------------------------- */
  186. static long testIt (ControlHandle control, Point myPoint)
  187.  
  188. {
  189.     Rect myRect;
  190.  
  191.     myRect = (*control)->contrlRect;
  192.     
  193.     if    (
  194.         ((*control)->contrlVis != 0) &&
  195.         ((*control)->contrlHilite != 255) &&
  196.         (PtInRect(myPoint,&myRect))
  197.         )
  198.         return(inButton);
  199.     else
  200.         return(0);
  201. }
  202.